package edu.upn.talbd.proventas.dal;

import edu.upn.talbd.proventas.entidades.Categoria;
import edu.upn.talbd.proventas.entidades.Cliente;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class CategoriaDAO {
    public void registrar(Categoria objCategoria) {
        Connection con = ConnectionManager.getConnection();
        try {
            
            //2. Crear Statement JDBC
            Statement stmt = con.createStatement();
            //3. Crear Sentencias SQL
            String insert = ""
                    + "INSERT INTO categoria(nombre) "
                    + "VALUES(" 
                    + "'" + objCategoria.getNombre() + "')";
            stmt.executeUpdate(insert);
            //4. ResultSet
            //5. Cerrar conexion
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
            }
        }
    }
    
    public String eliminar(int idCategoria) {
        
        Connection con = ConnectionManager.getConnection();
        try {
            
            //2. Crear Statement JDBC
            Statement stmt = con.createStatement();
            //3. Crear Sentencias SQL
            //String delete = "delete from telefonoCliente where idCliente = " + idCliente;
            //stmt.executeUpdate(delete); 
            
            String delete = "delete from categoria where id = " + idCategoria;
            stmt.executeUpdate(delete); 
            
            
            
            //5. Cerrar conexion
            stmt.close();
            return "Categora eliminada"; 
        } catch (Exception e) {
            e.printStackTrace();
            return "Error al eliminar categoria"; 
        } finally {
            try {
                con.close();
            } catch (Exception e) {
            }
        }
    }
    
    public void modificar(Categoria objCategoria) {
        Connection con = ConnectionManager.getConnection();
        try {
            
            //2. Crear Statement JDBC
            Statement stmt = con.createStatement();
            //3. Crear Sentencias SQL
            String update = ""
                    + "update categoria c set "
                    + "c.nombre = '" + objCategoria.getNombre() + "' where c.id = " + objCategoria.getId();
            stmt.executeUpdate(update);
            
            //4. ResultSet
            //5. Cerrar conexion
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
            }
        }
    }
    
    public ArrayList<Categoria> buscar(String nombre) {
        Connection con = ConnectionManager.getConnection();
        try {
           
            //2. Crear Statement JDBC
            Statement stmt = con.createStatement();
            //3. Crear Sentencias SQL
            //Un objeto ResultSet, almacena los datos de resultados de una consulta
            ResultSet tabla = stmt.executeQuery("SELECT c.id, c.nombre "
                                            + "FROM categoria c "
                                            + "WHERE c.nombre like '%" + nombre + "%' ORDER BY c.id");
            
            //System.out.println("Codigo\tNombre");
            ArrayList<Categoria> coleccion = new ArrayList<Categoria>();
            while(tabla.next()){
                Categoria obj = new Categoria();
                obj.setId(tabla.getInt(1));
                obj.setNombre(tabla.getString(2));
                coleccion.add(obj);
                //System.out.println(tabla.getInt(1)+"\t"+tabla.getString(2));
            }
            //4. ResultSet
            //5. Cerrar conexion
            stmt.close();
            return coleccion;
        } catch (Exception e) {
            e.printStackTrace(); 
            ArrayList<Categoria> c = new ArrayList<Categoria>();
            return c;
        } finally {
            try {
                con.close();
            } catch (Exception e) {
            }
        }
    }
    
    public ArrayList<Categoria> buscar() {
        Connection con = ConnectionManager.getConnection();
        try {
           
            //2. Crear Statement JDBC
            Statement stmt = con.createStatement();
            //3. Crear Sentencias SQL
            //Un objeto ResultSet, almacena los datos de resultados de una consulta
            ResultSet tabla = stmt.executeQuery("SELECT c.id, c.nombre "
                                            + "FROM categoria c ORDER BY c.id");
            
            //System.out.println("Codigo\tNombre");
            ArrayList<Categoria> coleccion = new ArrayList<Categoria>();
            while(tabla.next()){
                Categoria obj = new Categoria();
                obj.setId(tabla.getInt(1));
                obj.setNombre(tabla.getString(2));
                coleccion.add(obj);
                //System.out.println(tabla.getInt(1)+"\t"+tabla.getString(2));
            }
            //4. ResultSet
            //5. Cerrar conexion
            stmt.close();
            return coleccion;
        } catch (Exception e) {
            e.printStackTrace(); 
            ArrayList<Categoria> c = new ArrayList<Categoria>();
            return c;
        } finally {
            try {
                con.close();
            } catch (Exception e) {
            }
        }
    }
    
}
